﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace FlashGames
{
    public class Category
    {
        private static int nextId;
        public int ID { get; private set; }
        public string Name { get; set; }
        public string Url { get; set; }
        public string ImageUrl { get; set; }
        public string SubCategoryRegex { get; set; }
        public Site ParentSite { get; set; }
        public Category ParentCategory { get; set; }
        public bool HasSubCategories { get; set; }
        public bool SubCategoryDiscovered { get; set; }
        public bool GamesDiscovered { get; set; }
        public bool HasGames { get; set; }
        private List<Game> _games = new List<Game>();
        private List<Category> _subCategories = new List<Category>();

        public List<Game> Games
        {
            get { return _games; }
            set 
            { 
                _games = value;
                GamesDiscovered = true;
            }
        }

        public List<Category> SubCategories
        {
            get { return _subCategories; }
            set 
            { 
                _subCategories = value;
                SubCategoryDiscovered = true;
            }
        }

        public Category()
        {
            ID = Category.nextId;
            Category.nextId++;
        }

        public virtual List<Category> LoadCategories()
        {
            if (HasSubCategories)
            {
                string webData = Util.GetWebData(this.Url);
                try
                {
                  Match m = Regex.Match(webData, this.ParentSite.SubCategoryRegex, RegexOptions.Singleline | RegexOptions.IgnoreCase);

                  while (m.Success)
                  {
                    Category category = new Category();
                    category.Name = m.Groups["title"].Value;
                    category.ImageUrl = m.Groups["imageUrl"].Value;
                    Util.DownloadThumb(category.ImageUrl, category.Name);
                    category.Url = m.Groups["url"].Value;
                    category.ParentCategory = this;
                    category.ParentSite = this.ParentSite;
                    if (!string.IsNullOrEmpty(category.ParentSite.GameRegex))
                      category.HasGames = true;
                    this.SubCategories.Add(category);

                    m = m.NextMatch();
                  }
                }
                catch
                {
                }
            }
            else LoadGames();
            return SubCategories;
        }

        public virtual List<Game> LoadGames()
        {
            if (this.ParentSite.HasGames)
            {
                string webData = Util.GetWebData(this.Url);
                try
                {
                  Match m = Regex.Match(webData, this.ParentSite.GameRegex, RegexOptions.IgnoreCase | RegexOptions.Singleline);

                  while (m.Success)
                  {
                    Game game = new Game();
                    game.Name = m.Groups["title"].Value;
                    game.ImageUrl = m.Groups["imageUrl"].Value;
                    Util.DownloadThumb(game.ImageUrl, game.Name);
                    game.Url = m.Groups["url"].Value;
                    game.ParentCategory = this;
                    game.ParentSite = this.ParentSite;
                    this.Games.Add(game);
                    this.GamesDiscovered = true;

                    m = m.NextMatch();
                  }
                }
                catch
                {

                }
            }
            return Games;
        }

        public virtual MediaPortal.GUI.Library.GUIListItem ToGUIListItem()
        {
            MediaPortal.GUI.Library.GUIListItem Item = new MediaPortal.GUI.Library.GUIListItem();
            Item.Label = this.Name;
            Item.Path = this.Url;
            //Item.Thumbnail = this.Image;
            Item.AlbumInfoTag = this;
            return Item;
        }

        public Image Image
        {
            get
            {
                return Util.GetImageFromUrl(this.ImageUrl); ;
            }
            set
            {
                Image = value;
            }
        }
    }
}
